var $edit=$("#edit_button");

function editDlg() {
    var rowIds = $table.jqGrid('getGridParam', 'selarrrow');
    var rowid = $table.getGridParam("selrow");
    var row = $table.jqGrid('getRowData', rowid);
    $('#edit_objectId').val(row.objectId).attr('readonly', true);
    $('#edit_objectName').val(row.objectName);
    $('#edit_abilities').val(row.abilities);
    $('#edit_objRelationship').val(row.objRelationship);
    $('#edit_chara').val(row.chara);
    $("#editDlg").modal();
}

function validateEdit() {
    return $("#form_edit").validate({
        rules: {
            objectId: {
                required: true,
                digits: true
            },
            objectName: {
                required: true
            },
            abilities: {
                required: true
            },
            objRelationship: {
                required: true
            },
            chara: {
                required: false
            }
        },
        messages: {
            objectId: {
                required: "请输入id",
                digits: "id必须是整数！"
            },
            objectName: {
                required: "请输入名称！"
            },
            abilities: {
                required: "请输入能力！"
            },
            objRelationship: {
                required: "请输入实体关系！"
            }
        }
    });
}

$edit.click(function () {
    if(validateEdit().form()) {
        edit();
    } else {
        // layer.msg("请检查格式！");
    }
});

function edit() {
    var form_data = $('#form_edit').serialize();
    var b_data = decodeURIComponent(form_data, true);//防止中文乱码
    var userdata = DataDeal.formToJson(b_data);//转化为json
    $.ajax({
        type: "post",
        url: "/object/edit",
        contentType: "application/json;charset=utf-8",
        data: userdata,
        success: function (data) {
            layer.msg(data);
            reload();
            $('#editDlg').modal('hide').on("hidden", function () {
                $(this).removeData("modal");
            });
            document.getElementById("form_edit").reset();//重置表单
        }
    });
}